/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.FornecedorController;
import dao.LogradouroDAO;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
import model.FornecedorMODEL;
import model.LogradouroMODEL;
import table.CellRenderer;
import table.TooltipCellRender;
import table.fornecedor.FornecedorTable;
import utilitarios.LimiteChar;

/**
 *
 * @author Junior Andrade
 */
public class Fornecedores extends javax.swing.JDialog {

    private static final int VISUALIZAR = 0;
    private static final int ADCIONAR = 1;
    private static final int EDITAR = 2;
    private static final int SELECIONAR = 3;

    private int status = VISUALIZAR;
    private int linhaSelecionada = -1;

    private List<FornecedorMODEL> fornecedores;

    private JTable tblFornecedores;
    private FornecedorTable tableModel;
    private JScrollPane scrollPane;

    /**
     * Creates new form Fornecedores
     * @param parent
     * @param modal
     */
    public Fornecedores(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        montaTabela();
        statusTela(status);
        limiteJTextFields();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTRazao = new javax.swing.JTextField();
        jTNome = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTRamal = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTEmail = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTRua = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jTBairro = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jTEstado = new javax.swing.JTextField();
        jTCidade = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jFTelefone = new javax.swing.JFormattedTextField();
        jFCnpj = new javax.swing.JFormattedTextField();
        jFCep = new javax.swing.JFormattedTextField();
        jTNum = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMNovo = new javax.swing.JMenu();
        jMSalvar = new javax.swing.JMenu();
        jMEditar = new javax.swing.JMenu();
        jMExcluir = new javax.swing.JMenu();
        jMLimpar = new javax.swing.JMenu();
        jMCancelar = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Formulário de Cadastro", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(51, 51, 51))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(51, 51, 51));
        jLabel1.setText("Razão social:");

        jTRazao.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jTNome.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(51, 51, 51));
        jLabel2.setText("Nome:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(51, 51, 51));
        jLabel3.setText("CNPJ:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(51, 51, 51));
        jLabel4.setText("Telefone:");

        jTRamal.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(51, 51, 51));
        jLabel5.setText("Ramal:");

        jTEmail.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(51, 51, 51));
        jLabel6.setText("E-mail:");

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(51, 51, 51));
        jLabel7.setText("CEP:");

        jTRua.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(51, 51, 51));
        jLabel8.setText("Rua:");

        jTBairro.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel9.setForeground(new java.awt.Color(51, 51, 51));
        jLabel9.setText("Bairro:");

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel11.setForeground(new java.awt.Color(51, 51, 51));
        jLabel11.setText("Estado:");

        jTEstado.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jTCidade.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel12.setForeground(new java.awt.Color(51, 51, 51));
        jLabel12.setText("Cidade:");

        try {
            jFTelefone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFTelefone.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        try {
            jFCnpj.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.###/####-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFCnpj.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        try {
            jFCep.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#####-###")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFCep.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jFCep.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jFCepKeyReleased(evt);
            }
        });

        jTNum.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel10.setForeground(new java.awt.Color(51, 51, 51));
        jLabel10.setText("Num:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jTRazao)
                                .addComponent(jTNome)
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addComponent(jFCnpj, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(jLabel4)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jFTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(jLabel5)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jTRamal, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jTEmail))
                            .addComponent(jFCep, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTRua))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel11)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTBairro, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(26, 26, 26)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel12)
                                .addGap(163, 163, 163))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(13, 13, 13)
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTNum, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTCidade, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addContainerGap(52, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTRazao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jTRamal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFCnpj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jFCep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jTRua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jTBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12)
                    .addComponent(jTCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jTEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 169, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        jMenuBar1.setBackground(new java.awt.Color(51, 51, 51));

        jMNovo.setForeground(new java.awt.Color(255, 255, 255));
        jMNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/Add.png"))); // NOI18N
        jMNovo.setText("Novo");
        jMNovo.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jMNovo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMNovoMouseClicked(evt);
            }
        });
        jMenuBar1.add(jMNovo);

        jMSalvar.setForeground(new java.awt.Color(255, 255, 255));
        jMSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/Save.png"))); // NOI18N
        jMSalvar.setText("Gravar");
        jMSalvar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jMSalvar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMSalvarMouseClicked(evt);
            }
        });
        jMenuBar1.add(jMSalvar);

        jMEditar.setForeground(new java.awt.Color(255, 255, 255));
        jMEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/Modify.png"))); // NOI18N
        jMEditar.setText("Alterar");
        jMEditar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jMEditar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMEditarMouseClicked(evt);
            }
        });
        jMenuBar1.add(jMEditar);

        jMExcluir.setForeground(new java.awt.Color(255, 255, 255));
        jMExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/Close.png"))); // NOI18N
        jMExcluir.setText("Deletar");
        jMExcluir.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jMExcluir.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMExcluirMouseClicked(evt);
            }
        });
        jMenuBar1.add(jMExcluir);

        jMLimpar.setBackground(new java.awt.Color(51, 51, 51));
        jMLimpar.setForeground(new java.awt.Color(255, 255, 255));
        jMLimpar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/Redo.png"))); // NOI18N
        jMLimpar.setText("Limpar");
        jMLimpar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jMLimpar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMLimparMouseClicked(evt);
            }
        });
        jMenuBar1.add(jMLimpar);

        jMCancelar.setBackground(new java.awt.Color(51, 51, 51));
        jMCancelar.setForeground(new java.awt.Color(255, 255, 255));
        jMCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/Back.png"))); // NOI18N
        jMCancelar.setText("Cancelar");
        jMCancelar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jMCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMCancelarMouseClicked(evt);
            }
        });
        jMenuBar1.add(jMCancelar);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 13, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMCancelarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMCancelarMouseClicked

        if (jMCancelar.isEnabled()) {
            limpaCampos();
            statusTela(VISUALIZAR);
        }
    }//GEN-LAST:event_jMCancelarMouseClicked

    private void jMLimparMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMLimparMouseClicked
        if (jMLimpar.isEnabled()) {
            limpaCampos();
        }
    }//GEN-LAST:event_jMLimparMouseClicked

    private void jMNovoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMNovoMouseClicked
        if (jMNovo.isEnabled()) {
            statusTela(ADCIONAR);
            jTRazao.requestFocus();
        }
    }//GEN-LAST:event_jMNovoMouseClicked

    private void jMSalvarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMSalvarMouseClicked
        if (jMSalvar.isEnabled()) {
            String razao = jTRazao.getText();
            String nome = jTNome.getText();
            String cnpj = jFCnpj.getText();
            String telefone = jFTelefone.getText();
            String ramal = jTRamal.getText();
            String email = jTEmail.getText();
            String cep = jFCep.getText().replace("-", "");
            String rua = jTRua.getText();
            String num = jTNum.getText();
            
            if(razao.trim().equals("")){
                JOptionPane.showMessageDialog(this, "Preencha o campo Razão Social!");
            } else if(nome.trim().equals("")){
                JOptionPane.showMessageDialog(this, "Preencha o campo Nome!");
            } else if(cnpj.replace(".", "").replace("/", "").replace("-", "").trim().equals("")){
                JOptionPane.showMessageDialog(this, "Preencha o campo CNPJ!");
            } else if(telefone.replace("(", "").replace(")", "").replace("-", "").trim().equals("")){
                JOptionPane.showMessageDialog(this, "Preencha o campo Telefone!");
            } else if(ramal.trim().equals("")){
                JOptionPane.showMessageDialog(this, "Preencha o campo Ramal!");
            } else if(email.trim().equals("")){
                JOptionPane.showMessageDialog(this, "Preencha o campo Email!");
            } else if(cep.trim().equals("")){
                JOptionPane.showMessageDialog(this, "Preencha o campo CEP!");
            } else if(rua.trim().equals("")){
                JOptionPane.showMessageDialog(this, "CEP Inválido!");
            } else if(num.trim().equals("")){
                JOptionPane.showMessageDialog(this, "Preencha o campo Número!");
            } else{
                if(status == ADCIONAR){
                    if(new FornecedorController().salvar(razao, nome, cnpj, telefone, ramal, email, Integer.parseInt(cep), num, tableModel)){
                        JOptionPane.showMessageDialog(this, "Fornecedor Cadastrado com Sucesso!");
                    }else{
                        JOptionPane.showMessageDialog(this, "Erro ao Cadastrar fornecedor!\nTente novamente mais tarde!");
                    }
                }else if(status == EDITAR){
                    int id = fornecedores.get(linhaSelecionada).getId();
                    if(new FornecedorController().alterar(id, razao, nome, cnpj, telefone, ramal, email, Integer.parseInt(cep), num, tableModel, linhaSelecionada)){
                        JOptionPane.showMessageDialog(this, "Fornecedor Editado com Sucesso!");
                    }else{
                        JOptionPane.showMessageDialog(this, "Erro ao Editar fornecedor!\nTente novamente mais tarde!");
                    }
                }
                statusTela(VISUALIZAR);
            }
            
            
        }
    }//GEN-LAST:event_jMSalvarMouseClicked

    private void jMEditarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMEditarMouseClicked
        if (jMEditar.isEnabled()) {
            linhaSelecionada = tblFornecedores.getSelectedRow();
            statusTela(EDITAR);
            FornecedorMODEL forn = fornecedores.get(linhaSelecionada);
            
            jTRazao.setText(forn.getRazaoSocial());
            jTNome.setText(forn.getNome());
            jFCnpj.setText(forn.getCnpj());
            jFTelefone.setText(forn.getTel());
            jTRamal.setText(forn.getRamal());
            jTEmail.setText(forn.getEmail());
            jFCep.setText(String.valueOf(forn.getLogradouro()));
            jTNum.setText(forn.getNum());
            jFCepKeyReleased(null);
        }
    }//GEN-LAST:event_jMEditarMouseClicked

    private void jMExcluirMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMExcluirMouseClicked
        if(jMExcluir.isEnabled()){
            linhaSelecionada = tblFornecedores.getSelectedRow();
            int excluir = JOptionPane.showConfirmDialog(this, "Deseja realmente Excluir esses Dados?", "Excluir", JOptionPane.YES_NO_OPTION);
            if (excluir == JOptionPane.YES_OPTION) {

                FornecedorMODEL forn = fornecedores.get(linhaSelecionada);
                if (new FornecedorController().excluir(forn.getId(), tableModel, linhaSelecionada)) {

                    JOptionPane.showMessageDialog(this, "Excluido com sucesso!");
                    statusTela(VISUALIZAR);
                } else {
                    JOptionPane.showMessageDialog(this, "Erro ao Excluir Funcionario!\n Tente Novamente mais tarde!");
                }
            }
        }
    }//GEN-LAST:event_jMExcluirMouseClicked

    private void jFCepKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jFCepKeyReleased
        String cep = jFCep.getText().replace("-", "").trim();
        if (cep.toCharArray().length == 8) {
            for (int i = 0; i < cep.toCharArray().length; i++) {

                if (String.valueOf(cep.charAt(i)).equals(" ")) {
                    return;
                }
            }

            LogradouroMODEL log = new FornecedorController().getLougradouro(Integer.parseInt(cep));
            jTRua.setText(log.getEndereco());
            jTCidade.setText(log.getCidade());
            jTBairro.setText(log.getBairro());
            jTEstado.setText(log.getUf());
            jTNum.requestFocus();

        }
    }//GEN-LAST:event_jFCepKeyReleased

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField jFCep;
    private javax.swing.JFormattedTextField jFCnpj;
    private javax.swing.JFormattedTextField jFTelefone;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMCancelar;
    private javax.swing.JMenu jMEditar;
    private javax.swing.JMenu jMExcluir;
    private javax.swing.JMenu jMLimpar;
    private javax.swing.JMenu jMNovo;
    private javax.swing.JMenu jMSalvar;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField jTBairro;
    private javax.swing.JTextField jTCidade;
    private javax.swing.JTextField jTEmail;
    private javax.swing.JTextField jTEstado;
    private javax.swing.JTextField jTNome;
    private javax.swing.JTextField jTNum;
    private javax.swing.JTextField jTRamal;
    private javax.swing.JTextField jTRazao;
    private javax.swing.JTextField jTRua;
    // End of variables declaration//GEN-END:variables

    private void statusTela(int status) {
        if (status == VISUALIZAR) {
            this.status = VISUALIZAR;
            jMNovo.setEnabled(true);
            jMSalvar.setEnabled(false);
            jMEditar.setEnabled(false);
            jMExcluir.setEnabled(false);
            jMLimpar.setEnabled(false);
            jMCancelar.setEnabled(false);
            tblFornecedores.setEnabled(true);
            linhaSelecionada = -1;
            tblFornecedores.getSelectionModel().clearSelection();

            limpaCampos();
        } else if (status == ADCIONAR) {
            this.status = ADCIONAR;
            jMNovo.setEnabled(false);
            jMSalvar.setEnabled(true);
            jMEditar.setEnabled(false);
            jMExcluir.setEnabled(false);
            jMLimpar.setEnabled(true);
            jMCancelar.setEnabled(true);
            tblFornecedores.setEnabled(false);
        } else if (status == EDITAR) {
            this.status = EDITAR;
            jMNovo.setEnabled(false);
            jMSalvar.setEnabled(true);
            jMEditar.setEnabled(false);
            jMExcluir.setEnabled(true);
            jMLimpar.setEnabled(true);
            jMCancelar.setEnabled(true);
            tblFornecedores.setEnabled(false);
        } else if (status == SELECIONAR) {
            this.status = SELECIONAR;
            jMNovo.setEnabled(true);
            jMSalvar.setEnabled(false);
            jMEditar.setEnabled(true);
            jMExcluir.setEnabled(true);
            jMLimpar.setEnabled(false);
            jMCancelar.setEnabled(false);

        }
        statusCampos(status);
    }

    private void statusCampos(int status) {
        if (status == VISUALIZAR) {

            jTRazao.setEditable(false);
            jTNome.setEditable(false);
            jFCnpj.setEditable(false);
            jFTelefone.setEditable(false);
            jTRamal.setEditable(false);
            jTEmail.setEditable(false);
            jFCep.setEditable(false);
            jTRua.setEditable(false);
            jTBairro.setEditable(false);
            jTCidade.setEditable(false);
            jTEstado.setEditable(false);
            jTNum.setEditable(false);
        } else if (status == EDITAR) {

            jTRazao.setEditable(true);
            jTNome.setEditable(true);
            jFCnpj.setEditable(false);
            jFTelefone.setEditable(true);
            jTRamal.setEditable(true);
            jTEmail.setEditable(true);
            jFCep.setEditable(true);
            jTRua.setEditable(false);
            jTBairro.setEditable(false);
            jTCidade.setEditable(false);
            jTEstado.setEditable(false);
            jTNum.setEditable(true);

        } else if (status == ADCIONAR) {

            jTRazao.setEditable(true);
            jTNome.setEditable(true);
            jFCnpj.setEditable(true);
            jFTelefone.setEditable(true);
            jTRamal.setEditable(true);
            jTEmail.setEditable(true);
            jFCep.setEditable(true);
            jTRua.setEditable(false);
            jTBairro.setEditable(false);
            jTCidade.setEditable(false);
            jTEstado.setEditable(false);
            jTNum.setEditable(true);

        }
        jMNovo.requestFocus();
    }

    private void limpaCampos() {

        jTRazao.setText("");
        jTNome.setText("");
        if (status != EDITAR) {
            jFCnpj.setText("");
        }
        jFTelefone.setText("");
        jTRamal.setText("");
        jTEmail.setText("");
        jFCep.setText("");
        jTRua.setText("");
        jTBairro.setText("");
        jTCidade.setText("");
        jTEstado.setText("");
        jTNum.setText("");

    }

    private void getListaFornecedores() {

        fornecedores = new FornecedorController().listaFornecedores();

    }

    private List<LogradouroMODEL> getLogradouro() {
        List<LogradouroMODEL> log = new ArrayList<LogradouroMODEL>();
        for (FornecedorMODEL forn : fornecedores) {
            try {
                log.add(new LogradouroDAO().getLogradouro(forn.getLogradouro()));

            } catch (SQLException ex) {
                ex.printStackTrace(System.out);
            }
        }

        return log;
    }

    //METODOS DA TABELA
    private void montaTabela() {
        getListaFornecedores();
        jPanel3.add(getTblFuncionarios());
        scrollPane.setBounds(5, 5, jPanel3.getWidth() - 10, jPanel3.getHeight() - 10);
    }

    private JScrollPane getTblFuncionarios() {
        if (tblFornecedores == null) {
            tblFornecedores = new JTable();
            // permite selecionar apenas uma linha de cada vez
            tblFornecedores.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            tblFornecedores.setDefaultRenderer(String.class, new CellRenderer());

            tblFornecedores.addMouseListener(new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent me) {
                    //        System.out.println(linhaSelecionada);
                    if ((me.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
                        if (tblFornecedores.isEnabled()) {

                            if (status == VISUALIZAR) {
                                statusTela(SELECIONAR);

                            }
                        }
                    } else if ((me.getModifiers() & MouseEvent.BUTTON2_MASK) != 0) {
                        JOptionPane.showMessageDialog(null, "Isso não é algo Legal!");
                    } else if ((me.getModifiers() & MouseEvent.BUTTON3_MASK) != 0) {
                        JPopupMenu menu = new JPopupMenu();
                        JMenuItem cliqueme = new JMenuItem("Copiar");
                        int col = tblFornecedores.columnAtPoint(me.getPoint());
                        int row = tblFornecedores.rowAtPoint(me.getPoint());
                        cliqueme.addActionListener(new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent ae) {
                                copyClipboard(String.valueOf(tableModel.get(row, col)));
                            }
                        });

                        menu.add(cliqueme);

                        menu.show(tblFornecedores, me.getX(), me.getY());
                    }
                }

            });
            tblFornecedores.setModel(getTableModel());
            defineRenderers();
            scrollPane = new JScrollPane(tblFornecedores);
        }
        return scrollPane;
    }

    private FornecedorTable getTableModel() {
        if (tableModel == null) {
            tableModel = new FornecedorTable(fornecedores, getLogradouro());

        }
        return tableModel;
    }

    private void defineRenderers() {
//        DefaultTableCellRenderer rendererCentro = new DefaultTableCellRenderer();
//        rendererCentro.setHorizontalAlignment(SwingConstants.CENTER);
//        DefaultTableCellRenderer rendererDireita = new DefaultTableCellRenderer();
//        rendererDireita.setHorizontalAlignment(SwingConstants.RIGHT);
//        DefaultTableCellRenderer rendererEsquerda = new DefaultTableCellRenderer();
//        rendererEsquerda.setHorizontalAlignment(SwingConstants.LEFT);

        JTableHeader header = tblFornecedores.getTableHeader();
        header.setPreferredSize(new Dimension(0, 35));
        TableColumnModel modeloDaColuna = tblFornecedores.getColumnModel();
        int[] tam = {35, 250, 100, 80, 70, 60, 100, 200, 65, 100, 60, 50};

        for (int i = 0; i < tableModel.getColumnCount(); i++) {
            modeloDaColuna.getColumn(i).setMaxWidth(tam[i]);
            modeloDaColuna.getColumn(i).setCellRenderer(new TooltipCellRender());

        }

    }

    private void copyClipboard(String texto) {
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        StringSelection selection = new StringSelection(texto);
        clipboard.setContents(selection, null);
    }

    private void limiteJTextFields() {
        jTRamal.setDocument(new LimiteChar(4));

    }

}
